package com.gaussian.property.admin.module.system.controller.system;

import com.gaussian.property.admin.module.system.service.SysLoginService;
import com.gaussian.property.admin.module.system.service.SysPermissionService;
import com.gaussian.property.admin.module.system.vo.system.UserInfo;
import com.gaussian.property.module.system.vo.RouterVo;
import com.gaussian.property.admin.module.system.vo.system.LoginBody;
import com.songqi.common.core.domain.R;
import com.songqi.common.security.model.JwtTokenPair;
import com.songqi.common.security.model.LoginUser;
import com.songqi.common.security.service.TokenService;
import com.songqi.common.web.swagger.PassToToken;
import com.songqi.common.web.request.controller.BaseController;
import com.gaussian.property.module.system.entity.SysMenu;
import com.gaussian.property.module.system.entity.SysUser;
import com.gaussian.property.module.system.service.ISysMenuService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author songqi(13776346982)
 */
@RestController
public class SysLoginController extends BaseController
{
    @Resource
    private SysLoginService loginService;

    @Resource
    private ISysMenuService menuService;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PassToToken
    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody)
    {
        // 生成令牌
        JwtTokenPair tokenPair = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return R.success(tokenPair.getAccessToken());
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public R getInfo(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser user = (SysUser) loginUser.getTokenUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setUser(user);
        userInfo.setPermissions(permissions);
        userInfo.setRoles(roles);
        return R.success(userInfo);
    }

    /**
     * 获取路由（菜单）信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public R<List<RouterVo>> getRouters(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        // 用户信息
        SysUser user = (SysUser) loginUser.getTokenUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        List<RouterVo> routerVos = menuService.buildMenus(menus);
        return R.success(routerVos);
    }
}
